export default (app) => {
    /**
     * 首字母大写
     * @param str 字符串
     * @example heheHaha
     * @return {string} HeheHaha
     */
    function capitalizeFirstLetter(str) {
      //return str.charAt(0).toUpperCase() + str.slice(1)
      return str;
    }
   
    /**
     * 对符合'xx/xx.vue'组件格式的组件取组件名
     * @param str fileName
     * @example abc/bcd/def/basicTable.vue
     * @return {string} BasicTable
     */
    function validateFileName(str) {
      return /^\S+\.vue$/.test(str) &&
        str.replace(/^\S+\/(\w+)\.vue$/, (rs, $1) => capitalizeFirstLetter($1))
    }
 


    const requireComponent = require.context('', true, /\.vue$/)
   
    //console.log(validateFileName('user/hello.vue'))
  // 找到组件文件夹下以.vue命名的文件，如果文件名为index，那么取组件中的name作为注册的组件名
    requireComponent.keys().forEach(filePath => {
  
      const componentConfig = requireComponent(filePath)
      var fileName = validateFileName(filePath)
      
      var path = '';
      const paths = filePath.split("/");
      if(paths.length > 2){
        for(var i=1; i<paths.length-1; i++){
         path =  path + '/' + paths[i];
        }

        path = path.substring(1) + "/";
      }

      fileName = path + fileName;
      console.log(fileName)
      const componentName = fileName
      app.component(componentName, componentConfig.default || componentConfig)
   
    })
  }
